package only.level5.railgun.ysartifactsimulation.handler;

import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;

public class YsArtifactHandler {
    private final Flux<String> publisher;

    public YsArtifactHandler(Consumer<FluxSink<String>> consumer) {
        this.publisher = Flux.create(consumer).share();
    }

    protected Mono<Void> handle(WebSocketSession session) {

        final Flux<WebSocketMessage> message = publisher
                .map(session::textMessage);

        return session.send(message);
    }
}
